package com.qf.qfcenterservice.api;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.common.bean.R;
import com.qf.common.dto.AreaDto;
import com.qf.common.feigns.AreaApi;
import com.qf.common.model.QfArea;
import com.qf.qfcenterservice.service.AreaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class AreaFeignApi implements AreaApi {

    @Autowired
    private AreaService areaService;

    @PostMapping("/api/area/list")
    @Override
    public R list(AreaDto areaDto) {
        //页码
        long current = areaDto.getPage().getCurrent();
        //每页条数
        long size = areaDto.getPage().getSize();
        //行政区名称
        String name = areaDto.getName();
        //行政区编码
        Integer code = areaDto.getCode();

        //分页查询 - mybatis-plus (注意分页拦截器)
        IPage<QfArea> page = new Page<>(current,size);
        LambdaQueryWrapper<QfArea> qw = new LambdaQueryWrapper<>();

//        if (!StringUtils.isEmpty(name)){
//            qw.like(QfArea::getName,name);
//        }
//        if (!StringUtils.isEmpty(code)){
//            qw.like(QfArea::getCode,code);
//        }

        qw.like(!StringUtils.isEmpty(name),QfArea::getName,name);
        qw.like(!StringUtils.isEmpty(code),QfArea::getCode,code);

        areaService.page(page,qw);

        log.info("----page:{}",page.getRecords());

        return R.ok(page);
    }
}
